//
//  PVDDetailFlushUploadView.swift
//  GreenDeal
//
//  Created by Moennig Q on 7/2/2023.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class SupportFunctionView: CustomBackView {
    let typeImageView = UIImageView()
    let titleLabel = ContentLabel(R.font.interSemiBold(size: 16))
    let subTitleLabel = ContentLabel(R.font.interRegular(size: 13), hexStr: "#292D32B2")
    let resultLabel = ContentLabel(R.font.interMedium(size: 14),hexStr: "#63666AFF")
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpviews() {
        //type image view
        typeImageView.image = R.image.jobdetail_support_flushupload_disabled_icon()
        self.addSubview(typeImageView)
        typeImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
        }
        //result label
        //抗压缩,抗拉伸能力最强，不会被压缩或拉伸
        resultLabel.setContentHuggingPriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        resultLabel.setContentCompressionResistancePriority(.required, for: NSLayoutConstraint.Axis.horizontal)
        self.addSubview(resultLabel)
        resultLabel.snp.makeConstraints { make in
            make.right.equalTo(-21)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerY.equalToSuperview()
        }
        //title label
        titleLabel.text = "Flush Uploads"
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(typeImageView.snp.right).offset(12)
            make.height.width.greaterThanOrEqualTo(0)

        }
        subTitleLabel.text = "Upload remaining data"
        subTitleLabel.numberOfLines = 0
        self.addSubview(subTitleLabel)
        subTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.left.equalTo(titleLabel)
            make.right.equalTo(resultLabel.snp.left)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-10)

        }
        self.bindData()
    }
    func bindData(){
        
    }
}
class JobDetailSupportFunctionView: SupportFunctionView {
    fileprivate lazy var disposeBag = DisposeBag()
    override func bindData(){
        //1.监听panel数据的变化
        JobDetailModelHelper.shared.pvd_serial_numbers.subscribe {[weak self] _ in
            self?.setCountOfLocalData()
        }.disposed(by: self.disposeBag)
        //2.监听panel数据的变化
        JobDetailModelHelper.shared.pvd_inverter_serial_numbers.subscribe { [weak self] _ in
            self?.setCountOfLocalData()
        }.disposed(by: self.disposeBag)
        //3.监听panel数据的变化
        JobDetailModelHelper.shared.pvd_checklists.subscribe { [weak self] _ in
            self?.setCountOfLocalData()
        }.disposed(by: self.disposeBag)
        //4.监听signature数据的变化
        JobDetailModelHelper.shared.signatures.subscribe {[weak self] _ in
            self?.setCountOfLocalData()
        }.disposed(by: self.disposeBag)
    }
    //获取本地数据的数量
    func setCountOfLocalData(){
        let totalCnt =  UploadManager.shared.getTotalCntOfLocalData(JobDetailModelHelper.shared.jobDetailModel.id)
        if totalCnt > 0 {
            self.resultLabel.text = "#\(totalCnt)"
            self.typeImageView.image = R.image.jobdetail_support_flushupload_icon()
        }else{
            self.resultLabel.text = ""
            self.typeImageView.image = R.image.jobdetail_support_flushupload_disabled_icon()
        }
    }
}
class ServiceDetailSupportFunctionView: SupportFunctionView {
    fileprivate lazy var disposeBag = DisposeBag()
    override func bindData(){
        //监听panel数据的变化
        ServiceDetailModelHelper.shared.checklists.subscribe { [weak self] _ in
            self?.setCountOfLocalData()
        }.disposed(by: self.disposeBag)
    }
    func setCountOfLocalData(){
        let totalCnt =  UploadManager.shared.getServiceTotalCntOfLocalData(ServiceDetailModelHelper.shared.serviceDetailModel.serviceID)
        if totalCnt > 0 {
            self.resultLabel.text = "#\(totalCnt)"
            self.typeImageView.image = R.image.jobdetail_support_flushupload_icon()
        }else{
            self.resultLabel.text = ""
            self.typeImageView.image = R.image.jobdetail_support_flushupload_disabled_icon()
        }
    }
}
